﻿using System;
using System.Linq;
using nRoute.Controllers;
using nRoute.Controllers.Mapping;
using nRoute.Samples.OfficerXcel.ViewServices;
using nRoute.Components.Composition;

namespace nRoute.Samples.OfficerXcel.Controllers
{
    [MapController("Shell/ShowBallon/{message}/")]
    public class ShellBallonController
        : Controller
    {
        private const string MESSAGE_FORMAT = "\"{0}\" Requested";

        private readonly ITimeoutNotificationViewService _notificationViewService;

        [ResolveConstructor]
        public ShellBallonController(ITimeoutNotificationViewService notificationViewService)
        {
            if (notificationViewService == null) throw new ArgumentNullException("notificationViewService");
            _notificationViewService = notificationViewService;
        }

        public void Index(string message)
        {
            if (string.IsNullOrEmpty(message)) return;
            _notificationViewService.ShowNotification(string.Format(MESSAGE_FORMAT, SpaceMessage(message)), TimeSpan.FromSeconds(5));
        }

#region Helpers

        private static string SpaceMessage(string message)
        {
            if (string.IsNullOrEmpty(message)) return null;

            var _chars = message.ToCharArray().Where(Char.IsUpper).ToList();

            foreach (var _char in _chars)
            {
                message = message.Replace(_char.ToString(), " " + _char);
            }

            return message.Trim().Replace("  ", " ");
        }

#endregion

    }
}